package org.threebitsoftware.tbb.Contact_GUI;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.threebitsoftware.dao.generated.Contactphoto;
import org.threebitsoftware.tbb.dao.Contact;
import org.threebitsoftware.tbb.engine.EngineUtil;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.ImageData;

public class PhotoExport extends Dialog {
	
	private static Logger logger = Logger.getLogger(PhotoExport.class);
	
	private Text pathText_;
	private Shell shlContactPhotoExport;
	private Object result_;
	private Contact currentContact;
	
	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public PhotoExport(Shell parent, int style, Contact selectedContact) {
		super(parent, style);
		setText("SWT Dialog");
		currentContact = selectedContact;
	}
	
	public PhotoExport (Shell parent, int style){
		super(parent, style);
	}

	public Object open() {
		createContents();
		shlContactPhotoExport.open();
		shlContactPhotoExport.layout();
		
		Display display = getParent().getDisplay();

		while (!shlContactPhotoExport.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result_;
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 * Create contents of the shell.
	 */
	protected void createContents() {
		shlContactPhotoExport = new Shell(getParent(), SWT.DIALOG_TRIM
				| SWT.APPLICATION_MODAL);
		shlContactPhotoExport.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		shlContactPhotoExport.setSize(473, 87);
		shlContactPhotoExport.setText("Export Current Contact Photo");
		
		/**
		 *  handle the path text box
		 */
		{
			pathText_ = new Text(shlContactPhotoExport, SWT.BORDER);
			pathText_.setBounds(64, 7, 321, 19);
		}
		
		/**
		 * handle the export button
		 */
		{
			Button btnExport = new Button(shlContactPhotoExport, SWT.NONE);
			btnExport.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					
					if (currentContact==null ){
						JOptionPane.showMessageDialog(null,"Please sellect one contact.",
								null, 
								JOptionPane.NO_OPTION);
					} else if (pathText_.getText().isEmpty()){
						JOptionPane.showMessageDialog(null,"Please provide location of the photo.",
								null, 
								JOptionPane.NO_OPTION);
					} else {
						EngineUtil.getPhotoExport(currentContact, "org.threebitsoftware.tbb.module.photo.ContactPhoto", pathText_.getText());	
					}
					
				}
			});
			btnExport.setBounds(214, 32, 68, 23);
			btnExport.setText("E&xport");
		}
		
		/**
		 * handle the path label
		 */
		{	Label lblPath = new Label(shlContactPhotoExport, SWT.NONE);
			lblPath.setBounds(10, 10, 48, 19);
			lblPath.setText("Path");
		}
		{
			Button btnCancel = new Button(shlContactPhotoExport, SWT.NONE);
			btnCancel.addMouseListener(new MouseAdapter() {
				/**
				 * on mouse click of cancel button close this gui
				 */
				@Override
				public void mouseDown(MouseEvent e) {
					shlContactPhotoExport.close();
				}
			});
			btnCancel.setBounds(140, 32, 68, 23);
			btnCancel.setText("&Cancel");
		}
		
		/**
		 * handle the browse button
		 */
		{
			Button btnBrowse = new Button(shlContactPhotoExport, SWT.NONE);
			btnBrowse.addMouseListener(new MouseAdapter() {
				
				/**
				 * Open file browser when the browse button is pressed
				 */
				@Override
				public void mouseDown(MouseEvent e) {
					FileDialog saveDialog = new FileDialog(new Shell(), SWT.SAVE);
					saveDialog.setFilterExtensions(new String[] {"*.jpg","*.gif"});
					saveDialog.open();
					pathText_.setText(saveDialog.getFilterPath() != "" ? 
							saveDialog.getFilterPath()+ "\\" + 
							saveDialog.getFileName() : "");
				}
			});

			btnBrowse.setBounds(389, 5, 68, 23);
			btnBrowse.setText("&Browse");
		}
	}
}
